package com.eddie.singleton.type5;

/**
 * @author Eddie
 * @date 2022/4/29 9:18
 */
public class SingletonTest05 {

    public static void main(String[] args) {
        Singleton instance = Singleton.getInstance();
        Singleton instance2 = Singleton.getInstance();
        System.out.println(instance.hashCode());
        System.out.println(instance2.hashCode());

    }

}

/**
 * 懒汉式（线程安全,同步代码块，不使用这种写法）
 */
class Singleton {

    private static Singleton instance;

    private Singleton() {

    }


    /**
     * 提供一个静态公有方法，当时用到该方法时，才去创建instance
     * 即懒汉式
     */
    public static Singleton getInstance() {
        if (instance == null) {
            synchronized (Singleton.class) {
                instance = new Singleton();
            }
        }
        return instance;
    }
}
